home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / amiga / c_src2_5.zoo / float.c < prev    next >
C/C++ Source or Header  |  1988-08-15  |  4KB  |  138 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24.  
  25. /*  WAM representation of floats: bits 0-2: tag (010); bits 3-7: absolute
  26.     value of exponent; bits 8-29: absolute value of mantissa; bit 30:
  27.     sign of exponent (1: negative); bit 31: sign of mantissa (1: negative).  */
  28.  
  29. #include "sim.h"
  30. #include "aux.h"
  31. #include <math.h>
  32.  
  33. #define Bit21        0x200000
  34. #define Bits16to20  0x1f0000
  35.  
  36. #define MIN(x, y)   (x > y ? y : x)
  37.  
  38. #define EXP_SIGN    0x40000000
  39. #define MANT_SIGN   0x80000000
  40. #define exp_magn(op)    (((unsigned)(op & 0xff)) >> 3)
  41. #define mant_magn(op)    (((unsigned)(op & 0x3fffff00)) >> 8)
  42.  
  43. double frexp(), ldexp();    /* C library routines */
  44.  
  45. /* "floatval" converts floats from the WAM representation to the machine
  46.    representation.                            */
  47.  
  48. double floatval(op)
  49. word op;
  50. {
  51.     register long exponent, exp_sign;
  52.     double fval; int exp;
  53.  
  54.     exp_sign = op & EXP_SIGN;
  55.     fval = (double)mant_magn(op); exponent = exp_magn(op);
  56.     if (exp_sign) exp = -exponent; else exp = exponent;
  57.     if (op & MANT_SIGN) fval = -fval;
  58.     fval = ldexp(fval, exp);
  59.     return fval;
  60. }
  61.  
  62. /* "makefloat" converts floats from the machine representation to the WAM
  63.    representation.                              */
  64.  
  65. word makefloat(op)
  66. double op;
  67. {
  68.     long exp_sign, mant_sign;
  69.     int exponent, exp_diff, nshift, num_gaps, int_op;
  70.     unsigned int mask;
  71.  
  72.     if (op < 0) {
  73.     mant_sign = MANT_SIGN;
  74.     op = -op;}
  75.     else mant_sign = 0;
  76.     op = frexp(op, &exponent);
  77.     if ((op == 0.0) || (exponent <= -32)) {int_op = 0; exponent = 0;}
  78.     else {
  79.     while ( !((int_op = (int)(op + 0.5)) & Bit21) && (exponent > -31)) { /* keep top 10 bits
  80.                                 clear for shifting */
  81.         mask = (int_op & Bits16to20) >> 16;
  82.         switch (mask) {
  83.         case 0: num_gaps = 6; break;
  84.         case 1: num_gaps = 5; break;
  85.         case 2:
  86.         case 3: num_gaps = 4; break;
  87.         case 4:
  88.         case 5:
  89.         case 6: 
  90.         case 7: num_gaps = 3; break;
  91.         case 8:
  92.         case 9:
  93.         case 10:
  94.         case 11:
  95.         case 12:
  96.         case 13:
  97.         case 14:
  98.         case 15: num_gaps = 2; break;
  99.         default: /* cases 16 to 31 */
  100.              num_gaps = 1; break;
  101.         };
  102.         exp_diff = exponent - (-31);
  103.         nshift = MIN(exp_diff, num_gaps);
  104.         switch (nshift) {
  105.         case 0: break;
  106.         case 1: op *= 2; break;
  107.         case 2: op *= 4; break;
  108.         case 3: op *= 8; break;
  109.         case 4: op *= 16; break;
  110.         case 5: op *= 32; break;
  111.         case 6: op *= 64; break;
  112.         default: printf("makefloat: unexpected multiplier %d\n", nshift);
  113.         };
  114.         exponent -= nshift;
  115.     };
  116.     };
  117.     if (exponent < 0) {
  118.     exponent = -exponent;
  119.     exp_sign = EXP_SIGN;
  120.     } else exp_sign = 0;
  121.     return ( ((word)int_op<<8) | (exponent<<3) | exp_sign | mant_sign | FLOAT_TAG);
  122. }
  123.  
  124. prettymuch_equal(op1, op2)
  125. double op1, op2;
  126. {
  127.     double min, diff;
  128.  
  129.     if ((op1 < 0.0 && op2 > 0.0) ||
  130.         (op2 < 0.0 && op1 > 0.0)) return 0;
  131.     if (op1<0) op1 = -op1;
  132.     if (op2<0) op2 = -op2;
  133.     diff = op1 - op2; if (diff < 0) diff = -diff;
  134.     min = MIN(op1, op2);
  135.     if (min == 0.0) return (op1 == op2);
  136.     else return ( (diff/min) < EPSILON);
  137. }
  138.